package com.topchain.utils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import org.bouncycastle.crypto.digests.SM3Digest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;

public class CommUtil {


    /**
     * 获取当前时间n年后的时间
     * @param year
     * @return
     */
    public static Date getDate(int year){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.YEAR,year);
        return calendar.getTime();
    }

    // 计算SM3摘要值
    public static String getDigestWithSM3(byte[] srcData) {
        SM3Digest sm3Digest = new SM3Digest();
        sm3Digest.update(srcData, 0, srcData.length);
        byte[] digest = new byte[sm3Digest.getDigestSize()];
        sm3Digest.doFinal(digest, 0);
        return HexUtil.encodeHexStr(digest);
    }

    // 计算SHA1摘要值
    public static String getDigestWithSHA1(byte[] srcData){
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return HexUtil.encodeHexStr(digest.digest(srcData));
    }

    // 生成一个8位随机字符
    public static String getRandomStr() {
        return RandomUtil.randomStringWithoutStr(8, "0123456789");
    }
}
